<?php
/**
 * CakeCMS - 日志管理
 * 功能模块:
 *
 * @author zzdboy
 * @version 1.0
 */
class LogsController extends AppController {
	public $components = array (
			'CheckUserPermission',
			'RequestHandler',
			'AdminLog' 
	);
	public $helpers = array (
			'Page' 
	);
	
	/**
	 * 初始化执行
	 *
	 * @see AppController::beforeFilter()
	 */
	public function beforeFilter() {
		parent::beforeFilter ();
		
		// 判断是否登录
		$this->CheckUserPermission->checkLogin ( $this );
		
		$this->set ( 'title_for_layout', 'CakeCMS后台管理系统' );
	}
	public function index() {
		$this->set ( 'title_for_layout', '日志管理' );
		
		// 加载css文件
		$this->set ( 'cssList', array (
				'calendar/jscal2.css',
				'calendar/border-radius.css',
				'calendar/win2k.css' 
		) );
		
		// 加载js文件
		$this->set ( 'scriptList', array (
				'calendar/calendar.js',
				'calendar/lang/en.js' 
		) );
		
		// 接收分页参数
		if (isset ( $this->params ['named'] ['page'] ) && is_numeric ( $this->params ['named'] ['page'] )) {
			if (floor ( $this->params ['named'] ['page'] ) < 1) {
				$curpage = 1;
			} else {
				$curpage = floor ( $this->params ['named'] ['page'] );
			}
		} else {
			$curpage = 1;
		}
		
		$this->loadModel ( "AdminLogs" );
		
		// limit
		$limit = ($curpage - 1) * $this->perpage . ', ' . $this->perpage;
		
		$username = isset ( $this->params ['named'] ['username'] ) ? $this->params ['named'] ['username'] : '';
		$realname = isset ( $this->params ['named'] ['realname'] ) ? $this->params ['named'] ['realname'] : '';
		$start_time = isset ( $this->params ['named'] ['start_time'] ) ? $this->params ['named'] ['start_time'] : '';
		$end_time = isset ( $this->params ['named'] ['end_time'] ) ? $this->params ['named'] ['end_time'] : '';
		
		// 整理搜索条件
		$conditions = array ();
		
		if (! empty ( $username )) {
			$conditions ['username'] = $username;
		}
		if (! empty ( $realname )) {
			$conditions ['realname'] = $realname;
		}
		if (! empty ( $start_time )) {
			$conditions ['create_time >='] = $start_time;
		}
		if (! empty ( $end_time )) {
			$conditions ['create_time <='] = $end_time;
		}
		
		$list = $this->AdminLogs->GetAdminRoleByAll ( $conditions, $limit );
		
		$this->set ( 'username', $username );
		$this->set ( 'realname', $realname );
		$this->set ( 'start_time', $start_time );
		$this->set ( 'end_time', $end_time );
		
		$this->set ( 'list', $list ['result'] );
		// 分页参数
		$this->set ( 'total', $list ['count'] );
		$this->set ( 'perpage', $this->perpage );
		$this->set ( 'curpage', $curpage );
	}
	
	/**
	 * 清理日志
	 */
	public function delAll() {
		if ($this->request->isPost ()) {
			$this->loadModel ( "AdminLogs" );
			if ($this->AdminLogs->delAll ()) {
				// 记录操作日志
				$this->AdminLog->addLog ( '清空日志' );
				
				$msg = array (
						'rtn_code' => 0,
						'content' => '清理日志成功!' 
				);
				echo json_encode ( $msg );
				exit ();
				return false;
			} else {
				$msg = array (
						'rtn_code' => 0,
						'content' => '清理日志失败!' 
				);
				echo json_encode ( $msg );
				exit ();
			}
		}
	}
}
?>